import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//汽车业务类
public class CarBusiness {

    public static List<SaloonCar> saloonCarList = new ArrayList<SaloonCar>();
    public static List<PassengerCar> passengerCarList = new ArrayList<PassengerCar>();
    public static List<Bus> busList = new ArrayList<Bus>();
    //借出去的车子的集合
    public static List<SaloonCar> rentsaloonCarList = new ArrayList<SaloonCar>();
    public static List<PassengerCar> rentpassengerCarList = new ArrayList<PassengerCar>();
    public static List<Bus> rentbusList = new ArrayList<Bus>();

    //主菜单
    public static void menu(){
        System.out.println("1:查看车辆,2:租赁,3:还车,4:报废,5:采购新车,6:查看租赁记录,9:退出");
        Scanner scanner = new Scanner(System.in);
        int N=scanner.nextInt();
        switch (N){
            case 1:
                CarBusiness.checkCars();
                break;
            case 2:
                CarBusiness.rentCar();
                break;
            case 3:
                CarBusiness.returnCar();
                break;
            case 4:
                CarBusiness.scrapCar();
                break;
            case 5:
                CarBusiness.addNewCar();
                break;
            case 6:
                CarBusiness.checkRentCarRecord();
                break;
            case 9:
                CarBusiness.quit();
                break;
            default:
                break;
        }
    }

    //查看车辆情况
    public static void checkCars() {
        System.out.println("\t\t车品牌\t车牌号\t座位数\t租金\t车型号");
        for (int i = 0; i < saloonCarList.size(); i++) {
            System.out.println("轿车\t" + saloonCarList.get(i).getBrand() + "\t" + saloonCarList.get(i).getLpNumber() +
                    "\t" + saloonCarList.get(i).getSeatNum() + "\t\t" + saloonCarList.get(i).getRent() + "\t\t" + saloonCarList.get(i).getModel());
        }
        for (int i = 0; i < passengerCarList.size(); i++) {
            System.out.println("客车\t" + passengerCarList.get(i).getBrand() + "\t" + passengerCarList.get(i).getLpNumber() +
                    "\t" + passengerCarList.get(i).getSeatNum() + "\t\t" + passengerCarList.get(i).getRent());
        }
        for (int i = 0; i < busList.size(); i++) {
            System.out.println("汽车\t" + busList.get(i).getBrand() + "\t" + busList.get(i).getLpNumber() +
                    "\t" + busList.get(i).getSeatNum() + "\t\t" + busList.get(i).getRent());
        }
        menu();
    }

    //租车
    public static void rentCar() {
        System.out.print("请输入要租赁的类型（轿车/客车/汽车）");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        if (str.equals("轿车")) {
            System.out.println("现有可租的轿车情况如下：");
            System.out.println("\t\t车品牌\t车牌号\t座位数\t租金\t车型号");
            for (int i = 0; i < saloonCarList.size(); i++) {
                System.out.println("轿车\t" + saloonCarList.get(i).getBrand() + "\t" + saloonCarList.get(i).getLpNumber() +
                        "\t" + saloonCarList.get(i).getSeatNum() + "\t\t" + saloonCarList.get(i).getRent() + "\t\t" + saloonCarList.get(i).getModel());
            }
            System.out.println("请选择要租赁的车辆，请输入车牌号");
            String lpnumber = scanner.next();
            boolean falg=false;
            for (int i = 0; i < saloonCarList.size(); i++) {
                if (lpnumber.equals(saloonCarList.get(i).getLpNumber())) {
                    System.out.println("你要租赁的车辆信息如下：");
                    System.out.println("轿车\t" + saloonCarList.get(i).getBrand() + "\t" + saloonCarList.get(i).getLpNumber() +
                            "\t" + saloonCarList.get(i).getSeatNum() + "\t\t" + saloonCarList.get(i).getRent() + "\t\t" + saloonCarList.get(i).getModel());
                    System.out.print("请输入租赁的天数：");
                    int days = scanner.nextInt();
                    int money = days * saloonCarList.get(i).getRent();
                    System.out.println("请支付:" + money);
                    System.out.println("用车愉快！");
                    rentsaloonCarList.add(saloonCarList.get(i));
                    saloonCarList.remove(i);
                    falg=true;
                }
            }
            if (falg){
                System.out.println("租车成功");
                System.out.println("按0返回主菜单");
                int n=scanner.nextInt();
                if (n==0){
                    menu();
                    System.exit(1);
                }
            }else {
                    System.out.println("这辆车暂时没得租哦。。。");
                    System.out.println("是否继续重新租车(按0重新租车)");
                    int n=scanner.nextInt();
                    if (n==0) {
                        rentCar();
                    }else {
                        menu();
                    }
            }
        } else if (str.equals("客车")) {
            System.out.println("现有可租的客车情况如下：");
            System.out.println("\t\t车品牌\t车牌号\t座位数\t租金\t车型号");
            for (int i = 0; i < passengerCarList.size(); i++) {
                System.out.println("客车\t" + passengerCarList.get(i).getBrand() + "\t" + passengerCarList.get(i).getLpNumber() +
                        "\t" + passengerCarList.get(i).getSeatNum() + "\t\t" + passengerCarList.get(i).getRent());
            }
            System.out.println("请选择要租赁的车辆，请输入车牌号");
            String lpnumber = scanner.next();
            boolean falg=false;
            for (int i = 0; i < passengerCarList.size(); i++) {
                if (lpnumber.equals(passengerCarList.get(i).getLpNumber())) {
                    System.out.println("你要租赁的车辆信息如下：");
                    System.out.println("轿车\t" + passengerCarList.get(i).getBrand() + "\t" + passengerCarList.get(i).getLpNumber() +
                            "\t" + passengerCarList.get(i).getSeatNum() + "\t\t" + passengerCarList.get(i).getRent() + "\t\t");
                    System.out.print("请输入租赁的天数：");
                    int days = scanner.nextInt();
                    int money = days * passengerCarList.get(i).getRent();
                    System.out.println("请支付:" + money);
                    System.out.println("用车愉快！");
                    //passengerCarList.remove(passengerCarList.get(i));
                    rentpassengerCarList.add(passengerCarList.get(i));
                    passengerCarList.remove(passengerCarList.get(i));
                    falg=true;

                }
             }
            if (falg){
                System.out.println("租车成功!");
                System.out.println("按0返回主菜单");
                int n=scanner.nextInt();
                if (n==0){
                    menu();
                }else {
                    System.out.println("谢谢租车");
                    System.exit(1);
                }
            }else {
                System.out.println("这辆车暂时没得租哦。。。");
                System.out.println("是否继续重新租车(按0重新租车)");
                int n=scanner.nextInt();
                if (n==0){
                    rentCar();
                }
            }
            System.out.println("是否返回主菜单(按0返回)");
            int n=scanner.nextInt();
            if (n==0){
                menu();
            }else {
                System.out.println("谢谢租车");
                System.exit(1);
            }

        } else if (str.equals("汽车")) {
            System.out.println("现有可租的汽车情况如下：");
            System.out.println("\t\t车品牌\t车牌号\t座位数\t租金\t车型号");
            for (int i = 0; i < busList.size(); i++) {
                System.out.println("汽车\t" + busList.get(i).getBrand() + "\t" + busList.get(i).getLpNumber() +
                        "\t" + busList.get(i).getSeatNum() + "\t\t" + busList.get(i).getRent());
            }
            System.out.println("请选择要租赁的车辆，请输入车牌号");
            String lpnumber = scanner.next();
            boolean falg=false;
            for (int i = 0; i < busList.size(); i++) {
                if (lpnumber.equals(busList.get(i).getLpNumber())) {
                    System.out.println("你要租赁的车辆信息如下：");
                    System.out.println("轿车\t" + busList.get(i).getBrand() + "\t" + busList.get(i).getLpNumber() +
                            "\t" + busList.get(i).getSeatNum() + "\t\t" + busList.get(i).getRent() + "\t\t");
                    System.out.print("请输入租赁的天数：");
                    int days = scanner.nextInt();
                    int money = days * busList.get(i).getRent();
                    System.out.println("请支付:" + money);
                    System.out.println("用车愉快！");
                    //busList.remove(i);
                    rentbusList.add(busList.get(i));
                    busList.remove(i);
                    falg=true;
                }
            }if (falg){
                System.out.println("租车成功");
                System.out.println("按0返回主菜单");
                int n=scanner.nextInt();
                if (n==0){
                    menu();
                }else {
                    System.out.println("谢谢租车");
                    System.exit(1);
                }

            } else {
                System.out.println("这辆车暂时没得租哦。。。");
                System.out.println("是否继续重新租车(按0重新租车)");
                int n=scanner.nextInt();
                if (n==0){
                    rentCar();
                }
            }
        }
        System.out.println("是否返回主菜单(按0返回)");
        int n=scanner.nextInt();
        if (n==0){
            menu();
        }else {
            System.out.println("谢谢租车");
            System.exit(1);
        }
    }

    //还车
    public static void returnCar(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎前来还车!");
        System.out.println("请问你要还什么类型的车子(轿车/客车/汽车)");
        String str=scanner.next();
        if (str.equals("轿车")){
            System.out.println("请输入车牌号");
            String lpnumber=scanner.next();
            for (int i = 0; i <rentsaloonCarList.size(); i++) {
                if (lpnumber.equals(rentsaloonCarList.get(i).getLpNumber())){
                    System.out.println("还车成功，下次继续合作！谢谢你");
                    System.out.println("还车成功");

                    saloonCarList.add(rentsaloonCarList.get(i));
                    rentsaloonCarList.remove(i);
                    System.out.println("按0返回主菜单");
                    int n=scanner.nextInt();
                    if (n==0){
                        menu();
                    }
                    break;
                }else {
                    System.out.println("还车失败，根本就没有借这辆车出去！");
                    System.out.println("是否重新还车(按0还车否则退出去了喔)");
                    int n=scanner.nextInt();
                    if (n==0){
                        returnCar();
                    }else {
                        System.out.println("拜拜");
                        break;
                    }
                }
            }
        }else if (str.equals("客车")){
            System.out.println("请输入车牌号");
            String lpnumber=scanner.next();
            for (int i = 0; i <rentpassengerCarList.size(); i++) {
                if (lpnumber.equals(rentpassengerCarList.get(i).getLpNumber())){
                    System.out.println("还车成功，下次继续合作！谢谢你");
                    System.out.println("还车成功");
                    //rentpassengerCarList.remove(i);
                    passengerCarList.add(rentpassengerCarList.get(i));
                    rentpassengerCarList.remove(i);
                    System.out.println("按0返回主菜单");
                    int n=scanner.nextInt();
                    if (n==0){
                        menu();
                    }
                    break;
                }else {
                    System.out.println("还车失败，根本就没有借这辆车出去！");
                    System.out.println("是否重新还车(按0还车否则退出去了喔)");
                    int n=scanner.nextInt();
                    if (n==0){
                        returnCar();
                    }else {
                        System.out.println("拜拜");
                        break;
                    }
                }
            }
        }else if (str.equals("汽车")){
            System.out.println("请输入车牌号");
            String lpnumber=scanner.next();
            for (int i = 0; i <rentbusList.size(); i++) {
                if (lpnumber.equals(rentbusList.get(i).getLpNumber())){
                    System.out.println("还车成功，下次继续合作！谢谢你");
                    System.out.println("还车成功");
                    //rentbusList.remove(i);
                    busList.add(rentbusList.get(i));
                    rentbusList.remove(i);
                    System.out.println("按0返回主菜单");
                    int n=scanner.nextInt();
                    if (n==0){
                        menu();
                    }
                    break;
                }else {
                    System.out.println("还车失败，根本就没有借这辆车出去！");
                    System.out.println("是否重新还车(按0还车否则退出去了喔)");
                    int n=scanner.nextInt();
                    if (n==0){
                        returnCar();
                    }else {
                        System.out.println("拜拜");
                        break;
                    }

                }
            }
        }
    }

    //报废
    public static void scrapCar(){

    }

    //采购新车
    public static void addNewCar(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("你现在要添加哪种类型的车（1:轿车，2：客车，3：汽车）");
        int N = scanner.nextInt();
        while (scanner.hasNext()&&N>4&&N<0){
            System.out.println("输入错误,请按提示输入哦");
            N=scanner.nextInt();
        }
        if (N==1){
            System.out.println("请录入新车信息");
            System.out.println("输入新车品牌：");
            String brand=scanner.next();
            System.out.println("请输入车牌号：");
            String lgnumber=scanner.next();
            System.out.println("请设置租金:");
            int rent=scanner.nextInt();
            System.out.println("该车座位数多少");
            int seatnum=scanner.nextInt();
            System.out.println("该车型号多少");
            String model=scanner.next();
            saloonCarList.add(new SaloonCar(brand,lgnumber,seatnum,rent,model));
            System.out.println("添加成功！");
            System.out.println("是否继续添加新车(按0添加)");
            int n=scanner.nextInt();
            if (n==0){
                addNewCar();
            }else {
                System.out.println("是否返回主菜单(按0返回)");
                int m=scanner.nextInt();
                if (m==0){
                    menu();
                }
                System.out.println("拜拜");
            }
        }else if (N==2){
            System.out.println("请录入新车信息");
            System.out.println("输入新车品牌：");
            String brand=scanner.next();
            System.out.println("请输入车牌号：");
            String lgnumber=scanner.next();
            System.out.println("请设置租金:");
            int rent=scanner.nextInt();
            System.out.println("该车座位数多少");
            int seatnum=scanner.nextInt();
            passengerCarList.add(new PassengerCar(brand,lgnumber,seatnum,rent));
            System.out.println("添加成功！");
            System.out.println("是否继续添加新车(按0添加)");
            int n=scanner.nextInt();
            if (n==0){
                addNewCar();
            }else {
                System.out.println("是否返回主菜单(按0返回)");
                int m=scanner.nextInt();
                if (m==0){
                    menu();
                }
                System.out.println("拜拜");
            }
        }else if (N==3){
            System.out.println("请录入新车信息");
            System.out.println("输入新车品牌：");
            String brand=scanner.next();
            System.out.println("请输入车牌号：");
            String lgnumber=scanner.next();
            System.out.println("请设置租金:");
            int rent=scanner.nextInt();
            System.out.println("该车座位数多少");
            int seatnum=scanner.nextInt();
            busList.add(new Bus(brand,lgnumber,seatnum,rent));
            System.out.println("添加成功！");
            System.out.println("是否继续添加新车(按0添加)");
            int n=scanner.nextInt();
            if (n==0){
                addNewCar();
            }else {
                System.out.println("是否返回主菜单(按0返回)");
                int m=scanner.nextInt();
                if (m==0){
                    menu();
                }
                System.out.println("拜拜");
            }
        }
    }

    //查看租赁记录
    public static void checkRentCarRecord(){
        System.out.println("目前以下车辆租出去了喔");
        System.out.println("\t\t车品牌\t车牌号\t座位数\t租金\t车型号");
        for (int i = 0; i < rentsaloonCarList.size(); i++) {
            System.out.println("轿车\t" + rentsaloonCarList.get(i).getBrand() + "\t" + rentsaloonCarList.get(i).getLpNumber() +
                    "\t" + rentsaloonCarList.get(i).getSeatNum() + "\t\t" + rentsaloonCarList.get(i).getRent() + "\t\t" + rentsaloonCarList.get(i).getModel());
        }
        for (int i = 0; i < rentpassengerCarList.size(); i++) {
            System.out.println("客车\t" + rentpassengerCarList.get(i).getBrand() + "\t" + rentpassengerCarList.get(i).getLpNumber() +
                    "\t" + rentpassengerCarList.get(i).getSeatNum() + "\t\t" + rentpassengerCarList.get(i).getRent());
        }
        for (int i = 0; i < rentbusList.size(); i++) {
            System.out.println("汽车\t" + rentbusList.get(i).getBrand() + "\t" + rentbusList.get(i).getLpNumber() +
                    "\t" + rentbusList.get(i).getSeatNum() + "\t\t" + rentbusList.get(i).getRent());
        }
        System.out.println("是否返回主菜单（按0）");
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        if (n==0){
            menu();
        }
    }

    //退出系统
    public static void quit(){
        System.out.println("欢迎下次光临！");
        System.exit(1);
    }
}
